<template>
	<!-- 区域列表 -->
	<div class="content">
		<div class="content-top">
			区域管理_区域查询
		</div>
		<el-row>
			<el-col :span="4">
				<div class="dep-tree">
					<el-tree :data="data" :props="defaultProps" node-key="id" :default-expanded-keys="opendata" @node-click="handleNodeClick">
					</el-tree>
				</div>
			</el-col>
			<el-col :span="20">
				<div class="content-operation">
					<el-button type="primary" icon="el-icon-plus" @click='is_add = true' v-if="has_add">添加区域</el-button>
				</div>
				<div class="content-search">
					<el-form :inline="true" class="demo-form-inline" style="margin-top: 20px;">
						<el-form-item label="区域名称">
							<el-input v-model="dept" placeholder="区域名称"></el-input>
						</el-form-item>

						<el-button type="primary" @click='search' icon="el-icon-search">搜索</el-button>
						</el-form-item>
					</el-form>
				</div>
				<div class="content-table">
					<el-table :data="tableData" show-header stripe style="width: 100%;">
						<el-table-column prop="name" label="区域名称" width="200">
						</el-table-column>
						<el-table-column prop="code" label="部门编码" width="200">
						</el-table-column>
						<el-table-column prop="parentName" label="上级部门" width="200">
						</el-table-column>
						<el-table-column label="操作">
							<template slot-scope="scope">
								<el-button @click="handleClick(scope.row)" v-if="has_edit" type="text" size="small"><i class="el-icon-edit"></i></el-button>
								<el-button @click="handleClick1(scope.row)" v-if="has_del" type="text" size="small"><i class="el-icon-delete"></i></el-button>
							</template>
						</el-table-column>
					</el-table>
				</div>
				<div class="content-page">
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="page"
					 :page-size=limit layout="prev, pager, next, jumper" :total=total>
					</el-pagination>
				</div>
			</el-col>
		</el-row>
		<template v-if="is_add">
			<div class="all-screen"></div>
			<div class='add-box'>
				<AreaAdd @saveAdd='saveAdd'></AreaAdd>
			</div>
		</template>
		<template v-if="is_edit">
			<div class="all-screen"></div>
			<div class='add-box'>
				<AreaEdit @saveEdit='saveEdit' :id='id'></AreaEdit>
			</div>
		</template>
	</div>
</template>

<script>
	import AreaAdd from '@/components/area/AreaAdd.vue'
	import AreaEdit from '@/components/area/AreaEdit.vue'
	import Global from '@/components/Global.vue'
	export default {
		components: {
			AreaAdd: AreaAdd,
			AreaEdit: AreaEdit,
		},
		props:['power'],
		data() {
			return {
				id: 'id',
				is_add: false,
				is_edit: false,
				tableData: [],
				limit: 10,
				page: 1,
				total: 1,
				dept: '',
				opendata: [],
				data: [],
				defaultProps: {
					children: 'children',
					label: 'label'
				},
				has_add:false,
				has_edit:false,
				has_del:false
			}
		},
		mounted() {
			this.check_power();
			this.get_list();
			this.get_tree();
		},
		methods: {
			/*
			*检查权限
			*/
			check_power(){
				var self = this;
				this.power.forEach(function(element) {
					if(element.permissionName.indexOf("添加") != -1 )self.has_add = true;
					if(element.permissionName.indexOf("编辑") != -1 )self.has_edit = true;
					if(element.permissionName.indexOf("删除") != -1 )self.has_del = true;
				});
			},
			/*
			获取区域树
			*/
			get_tree() {
				var self = this;
				this.$axios.get(this.$global.serverIp + 'admin/sysProvince/findTree')
				  .then(function (res) {
					  if(res != '')
						self.data = self.change_data(res.data);
				  })
			},
			/*
			将区域列表转化为树
			*/
			change_data(list) {
				var data = [];
				var length = list.length;
				for (var i = 0; i < length; i++) {
					var e = {};
					var s_length = list[i].children.length;
					if (s_length > 0) {
						e.children = this.change_data(list[i].children);
					}
					e.id = list[i].id;
					e.label = list[i].name;
					data.push(e);
					this.opendata.push(list[i].id);
				}
				return data;
			},
			/*
			点击搜索按钮
			*/
			search() {
				this.page = 1;
				this.get_list();
			},
			/*
			点击区域树中的内容
			*/
			handleNodeClick(data) {
				this.dept = data.label;
				this.get_list();
			},
			/*
			获取区域列表
			*/
			get_list() {
				var self = this;
				this.$axios.post(this.$global.serverIp + 'admin/sysProvince/findPage', {
				  "limit": self.limit + "",
				  "page": self.page + "",
				  "name": self.dept
				  })
				  .then(function (res) {
					  if(res != ''){
						  self.tableData = res.data.list;
						  self.total = res.data.totalCount;
						  self.page = res.data.currPage;
					  }
						
				  })
			},
			/*
			添加弹出框关闭
			*/
			saveAdd(type) {
				if (type == 2) {
					this.get_list();
					this.get_tree();
				}
				this.is_add = false;
			},
			/*
			编辑弹出框关闭
			*/
			saveEdit(type) {
				if (type == 2) {
					this.get_list();
					this.get_tree();
				}
				this.is_edit = false;
			},
			/*
			点击页码
			*/
			handleCurrentChange(val) {
				this.page = val;
				this.get_list();
				console.log(`当前页: ${val}`);
			},
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
			},
			/*
			点击编辑
			*/
			handleClick(row) {
				this.id = row.id;
				this.is_edit = true;
			},
			/*
			点击删除
			*/
			handleClick1(row) {
				this.delopen(row);
			},
			/*
			删除
			*/
			delopen(row) {
				var self = this;
				this.$confirm('此操作将删除该区域, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
		
					$.ajax({
						url: this.$global.serverIp + 'admin/sysProvince/delete',
						data: '["' + row.id + '"]',
						xhrFields: {
							withCredentials: true
						},
						beforeSend : function(request) {
							request.setRequestHeader("Authorization", 'Bearer '+localStorage.getItem('token'));
						},
						contentType: 'application/json;charset=utf-8',
						dataType: 'json',
						type: 'delete',
						success: function(res) {
							if (res.code == Global.success_code) {
								self.$popup.open("删除成功");
								self.get_list();
								self.get_tree();
							} else {
								self.$popup.open(res.msg);
							}
						},
						error: function(res) {
							if(res.responseJSON.code == '040005'){
								self.$popup.open("登录过期");
							}else{
								self.$popup.open("请求失败");
							}
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			
		},

	}
	import '@/assets/css/common.css'
</script>
